package org.cyberdyne.jabbertalky;

import org.cyberdyne.jabbertalky.R;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;


public class SettingsActivity extends Activity
{
	public TextView tvJabServer;
	public TextView tvJabPort;
	public TextView tvJabUsername;
	public TextView tvJabPassword;
	public TextView tvIRCNickname;
	public TextView tvIRCServer;
	public TextView tvIRCChannel;
	private SharedPreferences settings;
 	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.xml.settings);		
	    loadSettings();
	}


	public void loadSettings()
	{
		settings = getSharedPreferences("JTSettings", Context.MODE_PRIVATE);

		TextView tvJabServer = (TextView)findViewById(R.id.jabserver);
		tvJabServer.setText(settings.getString("jabServer", "192.168.0.1"),TextView.BufferType.EDITABLE);
		
		TextView tvJabPort = (TextView)findViewById(R.id.jabport);
		int jabPort = settings.getInt("jabPort", 5222);
		tvJabPort.setText(Integer.toString(jabPort) ,TextView.BufferType.EDITABLE);		
		
		TextView tvJabUsername = (TextView)findViewById(R.id.jabusername);
		tvJabUsername.setText(settings.getString("jabUsername", "username"),TextView.BufferType.EDITABLE);

		TextView tvJabPassword = (TextView)findViewById(R.id.jabpass);
		tvJabPassword.setText(settings.getString("jabPassword", "password"),TextView.BufferType.EDITABLE);
		
		TextView tvIRCServer = (TextView)findViewById(R.id.ircserver);
		tvIRCServer.setText(settings.getString("ircServer", "radon.slashnet.org"),TextView.BufferType.EDITABLE);

		TextView tvIRCNickname = (TextView)findViewById(R.id.ircnickname);
		tvIRCNickname.setText(settings.getString("ircNickname", "JabberTalky"),TextView.BufferType.EDITABLE);
		
		TextView tvIRCChannel = (TextView)findViewById(R.id.ircchannel);
		tvIRCChannel.setText(settings.getString("ircChannel", "iB"),TextView.BufferType.EDITABLE);
	}


	public void saveSettings()
	{
		SharedPreferences.Editor editor  = settings.edit();

		TextView tvJabServer = (TextView)findViewById(R.id.jabserver);
	    editor.putString("jabServer", tvJabServer.getText().toString());

		TextView tvJabPort = (TextView)findViewById(R.id.jabport);	
	    editor.putInt("jabPort", Integer.parseInt(tvJabPort.getText().toString()));
	    
	    TextView tvJabUsername = (TextView)findViewById(R.id.jabusername);
	    editor.putString("jabUsername", tvJabUsername.getText().toString());
	    
		TextView tvJabPassword = (TextView)findViewById(R.id.jabpass);
	    editor.putString("jabPassword", tvJabPassword.getText().toString());
	    
		TextView tvIRCServer = (TextView)findViewById(R.id.ircserver);
	    editor.putString("ircServer", tvIRCServer.getText().toString());
	    
		TextView tvIRCNickname = (TextView)findViewById(R.id.ircnickname);
	    editor.putString("ircNickname", tvIRCNickname.getText().toString());
	    
		TextView tvIRCChannel = (TextView)findViewById(R.id.ircchannel);
	    editor.putString("ircChannel", tvIRCChannel.getText().toString());
	    
		editor.commit();
	}


	@Override
	public void onResume()
	{
		super.onResume();
		Log.v("XMPPClient", "Settings:onResume");
	}


	@Override
	protected void onStop()
	{
		super.onStop();
		Log.v("XMPPClient", "Settings:onStop");
		//saveSettings();
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ( keyCode == KeyEvent.KEYCODE_BACK )
		{
			saveSettings();
		}
		return super.onKeyDown(keyCode, event);
	}

}
